(function() {
  window.ProgressBar = function(options) {
    var _progressBar = this;

    _progressBar._options = {
      id: "",
      source: {
        dynamic: {
          url: "",
          params: null,
          success: $.noop
        },
        static: {
          data: [],
          success: $.noop
        },
        field: {
          data: "data",
          total: "total",
          title: "title",
          time: "time",
          percent: "percent",
          text: "text"
        },
        paging: false
      },
      color: "#1ab394"
    };

    _progressBar.settings(options);

    _progressBar.$obj = _progressBar.options.id.indexOf("#") == 0 ? $(_progressBar.options.id) : $("#" + _progressBar.options.id);
    _progressBar.$element = null;
    _progressBar._data = null;

    _progressBar._func = {
      _joinBar: function() {
        var _html = "";
        var _data = _progressBar._data;

        for(var i = 0; i < _data.length; i++) {
          _html += '<div class="progress">'
                   + '<div class="left">'
                   + '<span>' + _data[i][_progressBar._options.source.field.title] + '<br>'
                   + '截止日期：' + _data[i][_progressBar._options.source.field.time] + '</span>'
                   + '</div>'
                   + '<div class="right"><span>'
                   + '<div class="percent"><b>' + _data[i][_progressBar._options.source.field.percent] + '%</b></div>'
                   + '<div class="pbar ui-progressbar ui-widget ui-widget-content ui-corner-all" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0">'
                   + '<div class="ui-progressbar-value ui-widget-header ui-corner-left" style="display: none; width: calc(' + _data[i].percent + '% - 4px); background: ' + _progressBar.options.color + '; ">'
                   + '</div>'
                   + '</div>'
                   + '<div class="elapsed">' + _data[i][_progressBar._options.source.field.text] + '</div>'
                   + '</span></div>'
                   + '</div>';
        }

        return _html;
      }
    }

    _progressBar.create = function() {
      var $element = $('<div class="progressBars"></div>');

      _progressBar.$element = $element;
      _progressBar.$obj.append($element);

      return _progressBar;
    };

    _progressBar.load = function(cb) {
      var _func = _progressBar._func;
      var __static = _progressBar.options.source.static;
      var __data = __static.data;
      var __success = __static.success;

      if(!rapid.isEmpty(__data)) {
        _progressBar._data = __data;

        var _func = _progressBar._func;

        _progressBar.$element.append(_func._joinBar());

        typeof cb == "function" && cb(__data);

        typeof __success == "function" && __success(__data);
      } else {
        var __dynamic = _progressBar.options.source.dynamic;

        rapid.ajax({
          url: __dynamic.url,
          data: __dynamic.params,
          success: function(rs) {
            var d = rs[_progressBar.options.source.field.data];

            if(d && d instanceof Array && d.length > 0) {
              _progressBar._data = d;

              var _func = _progressBar._func;

              _progressBar.$element.append(_func._joinBar());

              typeof cb == "function" && cb(rs);
            }

            typeof __dynamic.success == "function" && __dynamic.success(rs);
          }
        });
      }
    }

    return _progressBar;
  };

  window.ProgressBar.prototype.render = function() {
    var _this = this;

    _this.create();
    _this.load(function(rs) {
      _this.$obj.append(_this.$element);
    });

    return _this;
  };

  window.ProgressBar.prototype.refresh = function() {
    var _this = this;

    _this.destory();
    _this.render();

    return _this;
  };

  window.ProgressBar.prototype.destory = function() {
    var _this = this;

    _this.$obj.html("");
    _this = null;
  };

  window.ProgressBar.prototype.settings = function(options) {
    var _this = this;

    _this.options = _this._options = $.extend(true, {}, _this._options, options);

    return _this;
  };
})()